import { BrowserWindow, shell } from 'electron'
import { join } from 'path'
import { is } from '@electron-toolkit/utils'
import icon from '../../../resources/icon.png?asset'
export default class MainFrame {
  #frame: unknown = null
  #width = 900
  #heingt = 670
  create() {
    // Create the browser window.
    this.#frame = new BrowserWindow({
      width: this.#width,
      height: this.#heingt,
      titleBarStyle: 'hidden', // 隐藏标题栏
      show: false,
      autoHideMenuBar: true,
      ...(process.platform === 'linux' ? { icon } : {}),
      webPreferences: {
        preload: join(__dirname, '../preload/index.js'),
        sandbox: false
      }
    })

    this.#frame.on('ready-to-show', () => {
      this.#frame.show()
    })

    this.#frame.webContents.setWindowOpenHandler((details) => {
      shell.openExternal(details.url)
      return { action: 'deny' }
    })

    // HMR for renderer base on electron-vite cli.
    // Load the remote URL for development or the local html file for production.
    if (is.dev && process.env['ELECTRON_RENDERER_URL']) {
      this.#frame.loadURL(process.env['ELECTRON_RENDERER_URL'])
    } else {
      this.#frame.loadFile(join(__dirname, '../renderer/index.html'))
    }
  }
  hide() {
    this.#frame.hide()
  }
  close() {
    this.#frame.close()
  }
  minimize() {
    this.#frame.minimize()
  }
  maximize() {
    this.#frame.minimize()
  }
  // 放大或缩小
  isFullScreen() {
    return this.#frame.isFullScreen()
  }
  setFullScreen(boole) {
    this.#frame.setFullScreen(boole)
  }
  setSize(x, y) {
    this.#frame.setSize(x, y)
  }

  setMinimumSize(x, y) {
    this.#frame.setMinimumSize(x, y)
  }

  center() {
    this.#frame.center()
  }

  setResizable(bool) {
    this.#frame.setResizable(bool)
  }
}
